#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define rept(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) rept(i,0,n)
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define il inline
#define db double
#define ld long double
#define re return
#define F first
#define S second
#define int long long
using namespace std;
template<typename T>void Mx(T &a,T b){if(a<b)a=b;}
template<typename T>void Mi(T &a,T b){if(a>b)a=b;}
const int MOD=998244353;
const int INF=1000000000000000000;
void FILEIO(string s){
    freopen((s+".in").c_str(),"r",stdin);
    freopen((s+".out").c_str(),"w",stdout);
}
const int N=200005,A=1000006;
int n,a[N],pf[N],las[A],dp[N],pl;
void run(){
    cin>>n;
    rept(i,1,n+1)cin>>a[i];
    a[0]=a[n+1]=0;
    rep(i,A)las[i]=-1;
    las[0]=0;las[a[1]]=1;
    rept(i,1,n+2)dp[i]=-INF;
    dp[1]=0;
    pf[1]=0;
    rept(i,2,n+2){
        pf[i]=pf[i-1]+a[i]*(a[i]==a[i-1]);
        if(las[a[i]]>=0&&a[i]!=a[i-1]){
            Mx(dp[i],dp[las[a[i]]+1]+a[i]+pf[i-1]-pf[las[a[i]]+1]);
        }
        Mx(dp[i],dp[i-pl-1]+a[i-1]*pl+a[i]*(a[i]==a[i-pl-2]));
        las[a[i]]=i;
        if(a[i]==a[i-1])pl++;
        else pl=0;
    }
    cout<<dp[n+1]<<"\n";
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    FILEIO("color");
    int T=1;
    cin>>T;
    while(T--)run();
    re 0;
}